﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Model.Entities;
using Model.Interfaces;
using Model.DAL;
using System.Data;

namespace Model.Repositories
{
    public static class DataRepository //: IDataRepository
    {
        //private WinterContext context;

        //public DataRepository(WinterContext context)
        //{
        //    this.context = context;
        //}

        public static ICollection<Page> GetAllPages()
        {
            using (var context = new WinterContext())
            {
                return context.Pages.ToList();
            }
        }

        public static Page GetPageByID(int ID)
        {
            using (var context = new WinterContext())
            {
                return context.Pages.Where(p=>p.ID==ID).First()  ;
            }
        }

        public static ICollection<PageDetail> GetAllPageDetails()
        {
            using (var context = new WinterContext())
            {
                return context.PageDetails.ToList();
            }
        }

        public static PageDetail GetPageDetailByID(int ID)
        {
            using (var context = new WinterContext())
            {
                return context.PageDetails.Where(p => p.ID == ID).First();
            }
        }

        public static ICollection<Content> GetAllContents()
        {
            using (var context = new WinterContext())
            {
                return context.Contents.ToList();
            }
        }

        public static Content GetContentByID(int ID)
        {
            using (var context = new WinterContext())
            {
                return context.Contents.Where(c => c.ID == ID).First();
            }
        }

        public static ICollection<News> GetAllNews()
        {
            using (var context = new WinterContext())
            {
                return context.News.ToList();
            }
        }

        public static News GetNewsByID(int ID)
        {
            using (var context = new WinterContext())
            {
                return context.News.Where(n => n.ID == ID).First();
            }
        }

        public static bool InsertData(object obj)
        {
            using (var context = new WinterContext())
            {
                context.Set(obj.GetType()).Add(obj);
                return context.SaveChanges() != 0 ;
            }
        }

        public static bool DeleteDataByID(int ID, object obj)
        {
            using (var context = new WinterContext())
            {
                context.Set(obj.GetType()).Remove(context.Set(obj.GetType()).Find(ID));
                return context.SaveChanges() != 0;
            }
        }

        public static bool DeleteData(object obj)
        {
            using (var context = new WinterContext())
            {
                context.Set(obj.GetType()).Remove(context.Entry(obj));
                return context.SaveChanges() != 0;
            }
        }

        public static bool UpdateData(object obj)
        {
            using (var context = new WinterContext())
            {
                context.Entry(obj).State = EntityState.Modified;
                return context.SaveChanges() != 0;
            }
        }

        //public void Save()
        //{
        //    context.SaveChanges();
        //}

        //private bool disposed = false;

        //protected virtual void Dispose(bool disposing)
        //{
        //    if (!this.disposed)
        //    {
        //        if (disposing)
        //        {
        //            context.Dispose();
        //        }
        //    }
        //    this.disposed = true;
        //}

        //public void Dispose()
        //{
        //    //Dispose(true);
        //    GC.SuppressFinalize(this);
        //}
    } 
}
